package net.arogarth.aws.android.manager.views;

import java.util.ArrayList;
import java.util.List;

import net.arogarth.aws.android.lib.AmazonRds;
import net.arogarth.aws.android.lib.AwsRequestException;
import net.arogarth.aws.android.lib.models.RdsDbInstance;
import net.arogarth.aws.android.manager.AwsManager;
import net.arogarth.aws.android.manager.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RdsDbInstanceList extends Activity {
	public class RdsDbInstanceListAdapter extends BaseAdapter {
		private final List<RdsDbInstance> instances = new ArrayList<RdsDbInstance>();

		private Context context;

		public RdsDbInstanceListAdapter(Context view, ListView list) {
			this.context = view;

			list.setAdapter(this);

			this.loadData();
		}

		private void loadData() {
			final ProgressDialog pd = ProgressDialog.show(this.context,
					"Please Wait...", "Loading Data...", true, false);

			final Thread t = new Thread() {
				public void run() {
//					while (true) {
					try {
						AmazonRds aec2 = new AmazonRds(
								AwsManager.getCurrentAwsAccount().getAccessKey(),
								AwsManager.getCurrentAwsAccount().getSecretKey());
						List<RdsDbInstance> _instances = aec2.descriptDbInstances();
						instances.clear();
						for (RdsDbInstance instance : _instances) {
							instances.add(instance);
						}

						handler.sendEmptyMessage(0);

						try {
							Thread.sleep(500);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} catch(final AwsRequestException e) {
						runOnUiThread(new Runnable() {
							public void run() {
								Toast.makeText(context, e.getMessage(),
										Toast.LENGTH_SHORT).show();
								finish();
							}
						});
					}
//					}
				}

				private Handler handler = new Handler() {
					@Override
					public void handleMessage(Message msg) {
						notifyDataSetChanged();
						pd.dismiss();
					}
				};
			};
			t.start();
		}

		public int getCount() {
			return this.instances.size();
		}

		public RdsDbInstance getItem(int arg0) {
			return (RdsDbInstance) this.instances.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final RdsDbInstance instance = (RdsDbInstance) getItem(position);

			final LinearLayout _ll = (LinearLayout) LayoutInflater.from(
					this.context).inflate(R.layout.rds_db_instance_list_item, parent, false);

			((TextView) _ll.findViewById(R.id.instanceId)).setText(instance.getDbInstanceIdentifier());
			
			((TextView) _ll.findViewById(R.id.tvSize)).setText(instance.getAllocatedStorage());
			((TextView) _ll.findViewById(R.id.tvState)).setText(instance.getDbInstanceStatus());
			((TextView) _ll.findViewById(R.id.tvType)).setText(instance.getDbInstanceClass());
			((TextView) _ll.findViewById(R.id.tvMultiAz)).setText(instance.getMultiAZ()?"Yes":"No");
			((TextView) _ll.findViewById(R.id.tvZone)).setText(instance.getAvailabilityZone());
			((TextView) _ll.findViewById(R.id.tvEngine)).setText(instance.getEngine());
			((TextView) _ll.findViewById(R.id.tvMaster)).setText(instance.getReadReplicaSourceDBInstanceIdentifier());

			
			if(instance.getDbInstanceStatus().equalsIgnoreCase("available")) {
				((ImageView) _ll.findViewById(R.id.instanceStatus)).setImageResource(R.drawable.greenled);
			} else {
				((ImageView) _ll.findViewById(R.id.instanceStatus)).setImageResource(R.drawable.redled);
			}

			return _ll;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.basic_list);
		final ListView lv = (ListView) findViewById(R.id.contentlist);
		new RdsDbInstanceListAdapter(RdsDbInstanceList.this, lv);
	}
}
